import { HTTP } from "../../utils/http"
import api from '../../utils/api.js'
import { config } from '../../utils/config'
import common from '../../utils/common'
let http = new HTTP()

// pages/pointsMall/pointsDetail/pointsDetail.js
Page({
  data: {
    imgUrl: config.imgUrl,
    page : 1,
    list: [],
    user_token: wx.getStorageSync('token'),
    date: common.date.format_month,  //现在的年月
    endMonth: common.date.format_month, //不能选择超出这个月的月份
    dotMonth: common.date.format_dot_month,
  },
  
  onLoad: function (options) {
    this.getCashCount();
    this.getCash(this.data.endMonth);
  },

  getCash(datatime){
      let that = this
      http.request({
        url: api.getCash,
        data: {
          token: this.data.user_token,
          page : this.data.page,
          month : datatime,
          type : 0
        },
        success: (res) =>{
          this.setData({
            list: that.data.list.concat(res.list),
            totalPage :  Math.ceil(res.total / res.per_page)
          })
        }
      })





    // let that = this
    // let data = {
    //   page : this.data.page,
    //   month : datatime,
    //   type : 0
    // };
    // util.getInfo(api.getCash, 'GET', data).then(res => {
        
    //     this.setData({
    //       list: that.data.list.concat(res.list),
    //       totalPage :  Math.ceil(res.total / res.per_page)
    //   })
    // })

  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let timeDay = e.detail.value;
    this.setData({
        page: 1,
        list: [],
        dotMonth: timeDay.replace('-','.'),
    })
},

 /*不可用余额*/
 toDisable(){
    wx.navigateTo({
        url : '/pages/moneyDetail/disable/disable'
    })
 },

 //获取余额
 getCashCount(user_token) {
        http.request({
            url: api.accountCoins,
            data: {
                token: user_token
            },
            success: (res) => {
                console.log(res)
                if (res.code == 200) {
                    this.setData({
                        cash: res.data.cash,
                        frozen_cash : res.data.frozen_cash
                    })
                }
            }
        })
    },
 
  onReachBottom: function() {
    console.log('触底刷新');
    if (this.data.page < this.data.totalPage) {
        this.setData({
            page: this.data.page + 1
        })
        this.getCash()
    }
}
})